package com.chen.integral.module.users.controller;

import com.chen.base.module.base.control.BaseController;
import com.chen.base.module.base.dto.Response;
import com.chen.entity.Users;
import com.chen.integral.module.users.service.UsersService;
import com.chen.integral.module.users.vo.UsersAddVo;
import com.chen.integral.module.users.vo.UsersDeleteVo;
import com.chen.integral.module.users.vo.UsersQueryVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: manager-spring-mvc-hb
 * @Description: 测试类
 * @Author: chenjiwei
 * @Date: 2020-11-15 15:04
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/integral/users")
public class UsersController extends BaseController {

    private Logger log = LoggerFactory.getLogger(UsersController.class);

    @Autowired
    private UsersService usersService;

    @PostMapping("/save")
    public Response save(@RequestBody UsersAddVo vo, BindingResult bindingResult){
        if(bindingResult.hasErrors())
            return Response.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        try {
            Users users = new Users();
            BeanUtils.copyProperties(vo, users);
            usersService.save(users);
            return Response.success("添加成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return Response.fail("添加失败");
        }
    }

    @PostMapping("/query")
    public Response query(@RequestBody UsersQueryVo vo){

        try {
            return usersService.query(vo);
        }catch (Exception e){
            log.info(e.getMessage());
            return Response.fail("查询失败");
        }
    }


    @PostMapping("/remove")
    // postman 中传输的内容 {"ids":[18,19]}
    public Response remove(@RequestBody UsersDeleteVo vo){
        try {
            usersService.deleteBatch(vo.getIds());
            return Response.success("删除成功");
        }catch (Exception e){
            log.info(e.getMessage());
            return Response.fail("删除失败");
        }
    }

}

